//
//  HomeViewController.m
//  Chat
//
//  Created by 任伟 on 16/1/3.
//  Copyright © 2016年 任伟. All rights reserved.
//

#import "HomeViewController.h"
#import "RWBlogModel.h"
#import "RWFrameModel.h"
#import "RWBlogCell.h"

@interface HomeViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSArray *microBlogFrames;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 300;
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.backgroundColor = [UIColor grayColor];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.microBlogFrames.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellID = @"cellID";
    RWBlogCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        
        cell = [[RWBlogCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    RWFrameModel *blogFrame = self.microBlogFrames[indexPath.row];
    
    cell.blogFrame = blogFrame;
    
    return cell;
}

// 用来设置不同的行数的cell的高度,这个方法会在返回怎样的cell之前调用.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    RWFrameModel *blogFrame = self.microBlogFrames[indexPath.row];
    
    return blogFrame.cellHeight;
}


#pragma mark - 懒加载数据
- (NSArray *)microBlogFrames {

    if (_microBlogFrames == nil) {
        
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"microBlogs.plist" ofType:nil ]];
    
        NSMutableArray *tempArray = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            RWFrameModel *blogFrame = [[RWFrameModel alloc] init];
            RWBlogModel *blogModel = [RWBlogModel blogWithDict:dict];
            blogFrame.blog = blogModel;
            [tempArray addObject:blogFrame];
        }
        
        _microBlogFrames = tempArray;
    }
    
    return _microBlogFrames;
}

@end
